﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

using System;

using Utils.WeChat.Options;

namespace Utils.WeChat
{
    public class WeChatApp
    {
        private readonly IOptionsMonitor<WeChatOptions> options;

        private readonly WeChatPay weChatPay;

        private readonly IServiceProvider serviceProvider;
        /// <summary>
        /// 可变配置
        /// </summary>
        /// <param name="options"></param>
        /// <param name="serviceProvider"></param>
        public WeChatApp(IOptionsMonitor<WeChatOptions> options, IServiceProvider serviceProvider)
        {
          
            this.options = options;
            this.weChatPay = ActivatorUtilities.CreateInstance<WeChatPay>(serviceProvider, options);
            this.serviceProvider = serviceProvider;
        }

        public WeChatMiniProgram GetWeChatMiniProgram(string appName)
        {
            if (!options.CurrentValue.Apps.ContainsKey(appName))
                throw Error.SystemError("WeChatApp: not find " + appName);
            return WeChatMiniProgram.GetInstance(serviceProvider, options.CurrentValue.Apps[appName]);
        }

        public WeChatPay GetWeChatPay()
        {
            return weChatPay;
        }
    }
}
